package com.delivery.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity; 
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; 
import android.os.Bundle; 
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window; 
import android.widget.CompoundButton; 
import android.widget.RadioButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.TabHost; 
 
public class MainTabActivity extends TabActivity implements OnCheckedChangeListener{ 
     
    private TabHost mTabHost; 
    
    private Intent mAIntent,mBIntent,mCIntent,mDIntent,mEIntent;
    
    public static Context mContext;   
	
	public static final int ABOUT = 1;
	
	public static final int EXIT = 2;

    private static Notification mNotification;   
    
    private static NotificationManager mNotificationManager = null;  
    
    private static final  int NOTIFICATION_ID = 0x0001;

	/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.maintabs); 
         
        this.mAIntent = new Intent(this,PicturesActivity.class); 
        this.mBIntent = new Intent(this,RecommendActivity.class); 
        this.mCIntent = new Intent(this,BeautyChannelActivity.class); 
        this.mDIntent = new Intent(this,NewActivity.class); 
        this.mEIntent = new Intent(this,ContactWebView.class); 
        
        ((RadioButton) findViewById(R.id.radio_button0)) 
        .setOnCheckedChangeListener(this); 
        ((RadioButton) findViewById(R.id.radio_button1)) 
        .setOnCheckedChangeListener(this); 
        ((RadioButton) findViewById(R.id.radio_button2)) 
        .setOnCheckedChangeListener(this); 
        ((RadioButton) findViewById(R.id.radio_button3)) 
        .setOnCheckedChangeListener(this); 
        ((RadioButton) findViewById(R.id.radio_button4)) 
        .setOnCheckedChangeListener(this); 
        setupIntent(); 
        mNotification = new Notification(R.drawable.icon,"非走兄弟",System.currentTimeMillis());   
	    //将使用默认的声音来提醒用户   
        //mNotification.defaults = Notification.DEFAULT_SOUND;   
	    mNotificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);  
	    mContext = MainTabActivity.this;
    } 
 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if(isChecked){ 
            switch (buttonView.getId()) { 
            case R.id.radio_button0: 
                this.mTabHost.setCurrentTabByTag("A_TAB"); 
                break; 
            case R.id.radio_button1: 
                this.mTabHost.setCurrentTabByTag("B_TAB"); 
                break; 
            case R.id.radio_button2: 
                this.mTabHost.setCurrentTabByTag("C_TAB"); 
                break; 
            case R.id.radio_button3: 
                this.mTabHost.setCurrentTabByTag("D_TAB"); 
                break; 
            case R.id.radio_button4: 
                this.mTabHost.setCurrentTabByTag("MORE_TAB"); 
                break; 
            } 
        } 
    } 
     
    private void setupIntent() { 
        this.mTabHost = getTabHost(); 
        TabHost localTabHost = this.mTabHost; 
        System.out.println(mAIntent);
        localTabHost.addTab(buildTabSpec("A_TAB", R.string.main_mryt, 
                R.drawable.icon_1_n, this.mAIntent)); 
 
        localTabHost.addTab(buildTabSpec("B_TAB", R.string.main_twtj, 
                R.drawable.icon_1_n, this.mBIntent)); 
 
        localTabHost.addTab(buildTabSpec("C_TAB", 
                R.string.main_mnpd, R.drawable.icon_1_n, 
                this.mCIntent)); 
 
        localTabHost.addTab(buildTabSpec("D_TAB", R.string.main_new_activity, 
                R.drawable.icon_1_n, this.mDIntent)); 
 
        localTabHost.addTab(buildTabSpec("MORE_TAB", R.string.main_contact, 
                R.drawable.icon_1_n, this.mEIntent)); 
 
    } 
     
    private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, 
            final Intent content) { 
        return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel), 
                getResources().getDrawable(resIcon)).setContent(content); 
    } 
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
//		menu.add(0,ABOUT,3,R.string.about);
		menu.add(0,EXIT,4,R.string.exit);
		return super.onCreateOptionsMenu(menu);
	}
    
    @Override 
	public boolean dispatchKeyEvent(KeyEvent event) { 
		systemExit(event);
	    return super.dispatchKeyEvent(event); 
	} 
	
	private  void systemExit(KeyEvent event) { 
		if (event.getKeyCode() == KeyEvent.KEYCODE_HOME) { 
			//confirmSysExit();
			//sysExit();
			
	    } else if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
	    		//confirmSysExit();
	    		
	        sysExit();
	    }
	}  
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
			
		if(item.getItemId() == ABOUT){
			
		}else if(item.getItemId() == EXIT){
			sysExit();
		}
		return super.onOptionsItemSelected(item);
	}
	
	/**  
	 * 最小化，回到桌面  
	 *   
	 */  
	
	private Intent intent;
	
	public  void goHome(Activity activity) {  
	   intent = new Intent();          
	   intent.setAction("android.intent.action.MAIN");   
	   intent.addCategory("android.intent.category.HOME");   
	   activity.startActivity(intent);   
	
	} 
	
	private void sysExit(){
        android.os.Process.killProcess(android.os.Process.myPid()); 
		System.exit(0);
		finish();
	}
	private void confirmSysExit(){
		AlertDialog.Builder alertbBuilder = new AlertDialog.Builder(this);   
        alertbBuilder.setIcon(R.drawable.icon).setTitle("您真的要退出吗？");//.setMessage("你确定要离开吗？");
        alertbBuilder.setPositiveButton("隐藏", new DialogInterface.OnClickListener() {   
        	public void onClick(DialogInterface dialog, int which) {   
        		 goHome(MainTabActivity.this);
             }   
         });   
         alertbBuilder.setNeutralButton("退出", new DialogInterface.OnClickListener() {   
        	public void onClick(DialogInterface dialog, int which) {   
                int nPid = android.os.Process.myPid();  
                cancelNotication();
                android.os.Process.killProcess(nPid); 
             }   
         });   
        
         alertbBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {   
        	 public void onClick(DialogInterface dialog, int which) {   
        		 dialog.cancel();   
             }   
         }).create();   
         alertbBuilder.show();  
	} 
	
	public static void notificationManager(){
			Intent mIntent = new Intent(mContext,MainTabActivity.class);   
	        //这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性   
	        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       
	        PendingIntent mContentIntent =PendingIntent.getActivity(mContext,0, mIntent, 0);   
	        //这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错.   
	        mNotification.setLatestEventInfo(mContext, "聚美音乐","", mContentIntent);   
	        //这里发送通知(消息ID,通知对象)   
	        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
	        
	}
	
	public static void cancelNotication(){
		if(mNotificationManager != null){
			mNotificationManager.cancel(MainTabActivity.NOTIFICATION_ID);
		}
	}
	
}
